package com.pinyougou.shop.service;


import com.pinyougou.pojo.TbSeller;
import com.pinyougou.sellergoods.service.SellerService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WF on 2019-11-12 10:44
 */
public class UserDetailServiceImpl implements UserDetailsService {
    private SellerService sellerService;

    public void setSellerService(SellerService sellerService) {
        this.sellerService = sellerService;
    }

    @Override
    public UserDetails loadUserByUsername(String sellerId) throws UsernameNotFoundException {
        //1.根据商家id查询商家对象
        TbSeller seller = sellerService.findSellerById(sellerId);
        //判断商家的店是否通过审核
        if(seller.getStatus().equals("1")){
            //定义一个权限集合
            List<GrantedAuthority> authorities=new ArrayList<>();
            //添加角色权限
            authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
            //需要返回一个User对象，系统自带的对象，1，用户名 2.密码 3.权限集合
            return new User(sellerId,seller.getPassword(),authorities);
        }
        return null;
    }
}
